package edu.spec.sms.controller;

import edu.spec.sms.model.Student;
import edu.spec.sms.model.lesson.StudentLesson;
import edu.spec.sms.service.iface.IStudentLessonService;
import edu.spec.sys.model.dto.MvcReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/studentLesson")
public class StudentLessonController {

    @Autowired
    private IStudentLessonService studentLessonService;

    @ResponseBody
    @RequestMapping("/listStudentByCon")
    public MvcReturn listStudentByCon(Integer lessonId, Integer signIn){
        List<StudentLesson> slList = studentLessonService.listStudentLessonByCon(lessonId, signIn);
        List<Student>  sList = new ArrayList<Student>();
        Student tempS = null;
        for(StudentLesson sl : slList){
            tempS = sl.getStudent();
            tempS.setSignInStatus(sl.getSignIn());
            sList.add(tempS);
        }
        return new MvcReturn(true, sList);
    }

    @RequestMapping("/singIn/{lessonId}")
    public String singIn(@PathVariable Integer lessonId, HttpSession session){
        Student student = (Student)session.getAttribute("user");
        if(student==null){
            return "student/bind";
        }
        Integer studentId = student.getId();
        Map m = studentLessonService.signIn(studentId, lessonId);
        session.setAttribute("message", m.get("message"));
        if((Boolean) m.get("success")==true){
            return "common/optSuc";
        }else {
            return "common/optFail";
        }
    }

}
